<template>

  <!--去结算-->
  <div style="position: absolute;bottom: 0;left: 0; width:100%;opacity: 1;background:white;z-index: 3">
    <!--<div style="width: 100%;height:100%;background:white;position: relative"></div>-->
    <div class="settle_accounts fixed_div" style="position:relative;z-index: 2;top:0">
      <div class="good_check cjy-checkbox" style="width: 4.1rem">
        <label><input class="aui-checkbox" type="checkbox" style="line-height:3rem ;vertical-align:middle;"
                      v-model="checked" @change="changeControltyp">&nbsp;全选</label>
      </div>

      <div class="total_con" style="">
        <h3>合计：<span class="allPrice">¥ {{totalPrice.toFixed(2)}}</span></h3>
        <p style="font-size: 0.6rem">总计 ¥{{ (parseFloat(totalPrice) + parseFloat(discountMoney)).toFixed(2)}} 优惠&nbsp;¥{{discountMoney.toFixed(2)}}</p>
      </div>

      <div id="to_settlement" class="to_settlement" style="width: 5rem" @click="goToConfirmOrder">
        去结算
      </div>
    </div>
  </div>
  <!--去结算 end-->
</template>

<script>
  import {MessageBox} from '@/ui-components'
  import {translateDetailData} from '@/utils/TranslateDetailDate'
  import {
    queryFoodCart,
    queryGoodsListNormal,
    queryPromotionInfo,
    queryPromotionInfoOffLine,
    queryFoodCartForCart,
    deleteFoodCart,
    addFoodCart,
    updateFoodCart
  } from '@/utils/ReturnAxios'

  export default {
    data() {
      return {
        checked: true
      }
    },
    props: {
      allCheck: {
        type: Boolean,
        default: true
      },
      queryCart: {
        type: Function,
        default: async () => {

          console.log(
            '成功')
        }
      }
    },
    created() {
      this.checked = this.allCheck
    },
    watch: {
      allCheck(data) {
        this.checked = this.allCheck
      },
    },
    methods: {
      async goToConfirmOrder() {
        var sessionId = localStorage.getItem('sessionId')
        sessionId = sessionId == 'undefined' ? '' : sessionId
        if (!sessionId || sessionId == 'undefined') {
          MessageBox('未登录', '请登先录再进行结算').then(
            () => {
              this.$router.push({path: 'login', query: {page: 'cartForFood'}})
            }
          )
          return
        }
        
        if ((parseFloat(this.totalPrice) == "0.00") && (parseFloat(this.discountMoney) == "0.00")) {
          MessageBox('没有可购买商品', '请勾选商品')
          return
        }

        await this.queryCart()
        setTimeout(() => {
          this.stockCheck()
        }, 20)
      },

      //校验库存
      async stockCheck() {
        var checkedCartList = this.checkedCartList
        checkedCartList = checkedCartList.map((item) => {
          return translateDetailData(item)
        })
        var checkedCartNormalList = checkedCartList.filter((item) => {
          return item.goodsType != 8
        }) // 普通已勾选商品列表
        if (checkedCartNormalList.length === 0) {
          MessageBox('没有可购买商品', '请勾选商品')
          return
        }
        var isPlace = checkedCartNormalList.filter((item) => {
          return item.goodsNum > item.lastStockCountNum
        })
        if (isPlace.length > 0) {
          MessageBox({
            title: "下单失败",
            message: "商品库存不足请联系店员",
            cancelButtonText: '再想想'
          })
          return
        }
        this.$router.push({
					path: 'confirmOrder',
					query: {
						"confirmOrderFood": true,
					}
				})
      },

      async deleFromCart(data) {
        var config = {}
        config.shoppingCartList = data
        await  this.$store.dispatch('lyj/deleCart', config)
        // this.$router.push('confirmOrder')
        this.$router.push({
					path: 'confirmOrder',
					query: {
						"confirmOrderFood": true,
					}
				})
      },

      async updataCart(data) {
        var config = data.map((item) => {
          return {
            addPrice:item.addPrice,
            goodsCode:item.goodsCode,
            rid: item.rid,
            goodsNum: item.goodsNum,
            serve: item.serve,
            workingInfo: item.workingInfo,
            goodsDeliveryType: item.goodsDeliveryType,
            preorder: false
          }
        })
        await this.$store.dispatch('lyj/updataCart', config)
      },


      changeControltyp() {
        setTimeout(() => {
          var data = 2
          var checked = this.checked
          if (checked) {
            data = 1
          }
          this.$emit('changeChecked', checked)
          this.$emit('changeControltyp', data)
        }, 20)


      },


    },
    computed: {
      totalPrice() {
        return parseFloat(this.buyMorePrice) + parseFloat(this.aftrerDiscountMoney) + parseFloat(this.totalWorkingFee)
      },
      buyMorePrice() {
        return this.$store.getters['lyj/buyMorePrice']
      },
      aftrerDiscountMoney() {
        return this.$store.state.lyj.aftrerDiscountMoney
      },
      totalWorkingFee() {
        return this.$store.state.lyj.totalWorkingFee
      },
      discountMoney() {
        return this.$store.state.lyj.discountMoney
      },
      checkedCartList() {
        return this.$store.state.lyj.checkedCartList
      }
    },

    components: {}
  }
</script>

<style scopend>
  .cjy-checkbox {
    width: auto;
    padding: 1rem 0.5rem 0 0.5rem;
  }
</style>
